package gameSite;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CustomerList
 */
@WebServlet("/CustomerList")
public class CustomerList extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Connection conn;
	Statement stmt = null;
	ResultSet rs = null;
	ArrayList<User> customerList; 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CustomerList() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String result = getCustomersList();
			PrintWriter out = response.getWriter();
			out.print(result);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	protected String getCustomersList(){
		try{
			//STEP 4: Execute a query
				String sql, result="";
				customerList = new ArrayList<User>();
				User customer;
				int count = 0;
		      System.out.println("Creating statement...");
		      stmt = RemoteConnection.getConnection().createStatement();
		      sql = "SELECT UserID, userName, firstName, lastName, address, postalCode FROM Users order by UserID";
		      rs = stmt.executeQuery(sql);
		      while (rs.next()){
		    	  customer = new User();
		    	  customer.setUserID(Integer.parseInt(rs.getString("UserID")));
		    	  customer.setFirstName(rs.getString("userName"));
		    	  customer.setLastName(rs.getString("lastName"));
		    	  customer.setAddress(rs.getString("address"));
		    	  customer.setPostalCode("postalCode");
		    	  customerList.add(customer);
		      }
		      rs.close();
		      stmt.close();
		      RemoteConnection.closeConnection();
		      for(User c : customerList){   	  	      
			      result += "<tr class=\"data\">" +
							"<td class=\"data\" width=\"30\">"+ (++count)+ "</td>" +
							"<td class=\"data\"><a href=\"AdminCustomerInformation?id="+c.getUserID()+"\">"
									+ c.getUserID()+"</a></td>" +
							"<td class=\"data\"><label >"+c.getUserName()+"</label></td>" +
							"<td class=\"data\"><label >"+c.getFirstName()+"</label></td>" +
							"<td class=\"data\"><label >"+c.getLastName()+"</label></td>" +
							"<td class=\"data\"><label>"+c.getAddress()+"</label></td>" +
							"<td class=\"data\"><label>"+c.getPostalCode()+"</label></td>" +
							"</tr>";
		      }
		      System.out.println(result);
		      return result;
		     
		}
		catch(SQLException se){
		      //Handle errors for JDBC
		      se.printStackTrace();
		   }
		return null;
		
	}

}
